package com.lky.controller;

import com.lky.domain.Planeinformation;
import com.lky.domain.Planetype;
import com.lky.domain.User;
import com.lky.service.PlaneinformationService;
import com.lky.service.PlanetypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 名称:PlanetypeController
 * 描述:PlanetypeController的控制类
 *
 * @version 1.0
 * @author:likaiyang
 * @datetime:2023-11-22 20:24
 */
@Controller
@RequestMapping("/planetype")
public class PlanetypeController {

    @Autowired
    private PlanetypeService planetypeService;

    //查询全部飞机类型
    @RequestMapping("/findAllPlanetype")
    public String findAllPlanetype(Model model){
        List<Planetype> planetypeList=planetypeService.findAllPlanetype();
        model.addAttribute("allplanetype",planetypeList);
        return "/planetype/allplanetype";
    }
    //查询全部飞机类型-普通用户
    @RequestMapping("/findAllPlanetype_new")
    public String findAllPlanetype_new(Model model){
        List<Planetype> planetypeList=planetypeService.findAllPlanetype();
        model.addAttribute("allplanetype",planetypeList);
        return "/planetype/allplanetype_new";
    }

    //从系统首页跳转到添加页面
    @RequestMapping("/toinsertplanetype")
    public String toinsertPlanetype(){
        return "/planetype/insertplanetype";
    }
    //添加用户，成功则再次执行查询全部用户
    @RequestMapping("/insertplanetype")
    public String insertPlanetype(Planetype planetype, Model model){
        try{
            int i=planetypeService.insertPlanetype(planetype);
            if(i>0){
                List<Planetype> planetypeList=planetypeService.findAllPlanetype();
                model.addAttribute("allplanetype",planetypeList);
                model.addAttribute("msg","添加飞机类型成功");
                return "/planetype/allplanetype";
            }
            else{
                model.addAttribute("msg","添加飞机类型失败");
                return "/planetype/allplanetype";
            }
        }catch (Exception e){
            List<Planetype> planetypeList=planetypeService.findAllPlanetype();
            model.addAttribute("allplanetype",planetypeList);
            model.addAttribute("msg","添加飞机类型失败，可能原因为飞机类型名称重复！");
            return "/planetype/allplanetype";
        }
    }

    //根据id查询，用于更新和删除
    @RequestMapping("/findPlanetypeById")
    public Planetype findPlanetypeById(@RequestParam(name = "id") Integer id){
        Planetype planetype=planetypeService.findPlanetypeById(id);
        return planetype;
    }
    //跳转到修改用户信息的页面
    @RequestMapping("toupdatePlanetypeById/{id}")
    public String toupdatePlanetypeById(@PathVariable(name = "id")Integer id, Model model){
        Planetype planetype=planetypeService.findPlanetypeById(id);
        model.addAttribute("planetype",planetype);
        return "/planetype/updateplanetype";
    }
    //根据id更新用户信息
    @RequestMapping("/updatePlanetype")
    public String updatePlanetype(Planetype planetype,Model model){
        try{
            int i=planetypeService.updatePlanetypeById(planetype);
            if(i>0){
                List<Planetype> planetypeList=planetypeService.findAllPlanetype();
                model.addAttribute("allplanetype",planetypeList);
                model.addAttribute("msg","修改飞机类型信息成功");
                return "/planetype/allplanetype";
            }
            else{
                List<Planetype> planetypeList=planetypeService.findAllPlanetype();
                model.addAttribute("allplanetype",planetypeList);
                model.addAttribute("msg","修改失败");
                return "/planetype/allplanetype";
            }
        }catch (Exception e){
            List<Planetype> planetypeList=planetypeService.findAllPlanetype();
            model.addAttribute("allplanetype",planetypeList);
            model.addAttribute("msg","修改飞机类型信息失败，可能原因为飞机类型名称重复");
            return "/planetype/allplanetype";
        }
    }

    //根据id删除
    @RequestMapping("/deletePlanetypeById/{id}")
    public String deletePlanetypeById(@PathVariable(name = "id")Integer id,Model model){
        try{
            int i=planetypeService.deletePlanetypeById(id);
            if(i>0){
                List<Planetype> planetypeList=planetypeService.findAllPlanetype();
                model.addAttribute("allplanetype",planetypeList);
                model.addAttribute("msg","删除飞机类型成功");
                return "/planetype/allplanetype";
            }
            else{
                List<Planetype> planetypeList=planetypeService.findAllPlanetype();
                model.addAttribute("allplanetype",planetypeList);
                model.addAttribute("msg","删除失败，可能原因为外键关联问题");
                return "/planetype/allplanetype";
            }
        }catch (Exception e){
            List<Planetype> planetypeList=planetypeService.findAllPlanetype();
            model.addAttribute("allplanetype",planetypeList);
            model.addAttribute("msg","删除失败，可能原因为外键关联问题");
            return "/planetype/allplanetype";
        }
    }

    //模糊查询
    @RequestMapping("/findPlanetypeByStr")
    public String findPlanetypeByStr(@RequestParam(value="searchStr") String searchStr,Model model){
        List<Planetype> planetypeList=planetypeService.findPlanetypeByStr(searchStr);
        model.addAttribute("allplanetype",planetypeList);
        return "/planetype/allplanetype";
    }
    //模糊查询
    @RequestMapping("/findPlanetypeByStr_new")
    public String findPlanetypeByStr_new(@RequestParam(value="searchStr") String searchStr,Model model){
        List<Planetype> planetypeList=planetypeService.findPlanetypeByStr(searchStr);
        model.addAttribute("allplanetype",planetypeList);
        return "/planetype/allplanetype_new";
    }
}
